﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using He.Common;
using System.Text;

namespace He.Web.manage.comment
{
    public partial class list : He.Web.UI.Manage
    {
        public int channel_id;                                //栏目ID
        public int pid;
        protected int psize = 12;                          //每页记录数
        protected int page;                                   //当前页
        protected int pcount;                                 //记录总数
        public Model.channel channel;
        protected void Page_Load(object sender, EventArgs e)
        {
            page = GetInt("page", 0);
            pid = GetInt("pid", 0);

            if (!int.TryParse(Request.Params["channel_id"] as string, out this.channel_id))
            {
                JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                return;
            }

            channel = new BLL.channel().GetModelByCache(channel_id);

            if (!IsPostBack)
            {
                chkLoginLevel("viewComment" + channel.id);

                CookieHelper.Set("LastListUrl", Request.RawUrl);    //保存最后一次访问的列表页Url

                RptBind();
            }
        }

        #region 数据绑定
        private void RptBind()
        {
            string strWhere = Comb_SqlTxt(this.channel_id, this.pid);
            BLL.comment bll = new BLL.comment();
            rptList.DataSource = bll.GetPageList(psize, page, strWhere, "id desc");
            rptList.DataBind();
            pcount = bll.GetRecordCount(strWhere);
        }
        #endregion

        #region 文章标题
        public string ViewTitle(int cid, int pid)
        {
            BLL.article abll = new BLL.article();
            Model.article amodel = abll.GetModel(pid);
            if (amodel != null)
            {
                return amodel.title;
            }
            else
            {
                return "已删除";
            }
        }
        #endregion

        #region 栏目标题
        public string ViewChannel(int cid)
        {
            return new BLL.channel().GetModel(cid).title;
        }
        #endregion

        #region 组合SQL语句
        public string Comb_SqlTxt(int cid, int pid)
        {
            StringBuilder strTemp = new StringBuilder();
            strTemp.AppendFormat("id>0 and channel_id={0} ", cid);
            if (pid > 0)
            {
                strTemp.AppendFormat("and pid={0}", pid);
            }
            return strTemp.ToString();
        }
        #endregion

        #region 组合URL语句
        protected string Comb_UrlTxt(int cid, int pid)
        {
            StringBuilder strTemp = new StringBuilder();
            if (cid > 0)
            {
                strTemp.Append("channel_id=" + cid + "&");
            }
            if (cid > 0)
            {
                strTemp.Append("pid=" + pid + "&");
            }
            return strTemp.ToString().Trim('&');
        }
        #endregion

        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            chkLoginLevel("delComment" + channel.id);
            BLL.comment bll = new BLL.comment();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((HiddenField)rptList.Items[i].FindControl("HiddenId")).Value);
                if (cb.Checked)
                {
                    Model.comment model = bll.GetModel(id);
                    bll.Delete(id);
                }
            }
            SaveLogs(string.Format("[{0}管理]批量删除评论", channel.manage_title));
            JscriptPrint("批量删除成功啦！", Request.RawUrl, "Success");
        }

        protected void lbtnAudit_Click(object sender, EventArgs e)
        {
            chkLoginLevel("auditComment" + channel.id);
            BLL.comment bll = new BLL.comment();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((HiddenField)rptList.Items[i].FindControl("HiddenId")).Value);
                if (cb.Checked)
                {
                    bll.UpdateField(id, "is_lock=0");
                }
            }
            SaveLogs(string.Format("[{0}管理]批量审核评论", channel.manage_title));
            JscriptPrint("批量审核成功啦！", Request.RawUrl, "Success");
        }
    }
}